package com.javaEE.gamepigeon.controller;


import com.javaEE.gamepigeon.entity.Competition;
import com.javaEE.gamepigeon.entity.CompetitionBrief;
import com.javaEE.gamepigeon.service.CompetitionService;
import com.javaEE.gamepigeon.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Api(tags = "竞赛相关接口")
@RestController
@RequestMapping("/competition")
public class CompetitionController {

    @Autowired
    private CompetitionService competitionService;

    @ExceptionHandler
    public Result exceptionHandle(Exception e){
        return Result.error("用户操作错误！",e.getMessage());
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name="publisherId",value="发布者的ID",required=true,paramType="query"),
            @ApiImplicitParam(name="competitionName",value="比赛的名字",required=true,paramType="query"),
            @ApiImplicitParam(name="nickName",value="昵称",paramType="query"),
            @ApiImplicitParam(name="officialUrl",value = "官网的Url",paramType = "query"),
            @ApiImplicitParam(name="infoDetail",value = "详细内容",paramType = "query"),
            @ApiImplicitParam(name="registerDetail",value = "报名内容",paramType = "query"),
//            @ApiImplicitParam(name="imgFile",value = "封面的图片",paramType = "form"),
//            @ApiImplicitParam(name="titleImg",value = "竞赛头像",paramType = "form")
    })
    @PostMapping("/add")
    @ApiOperation("添加新赛事")
    public Result addCompetition(Integer publisherId, String competitionName, @RequestParam(required = false) String nickName,
                                 @RequestParam(required = false) String officialUrl , @RequestParam(required = false) String infoDetail,
                                 @RequestParam(required = false) String registerDetail , @RequestParam(required = false)MultipartFile imgFile,
                                 @RequestParam(required = false) MultipartFile titleImg){
        if(competitionService.addCompetition(publisherId,competitionName,nickName,officialUrl,infoDetail,registerDetail,imgFile,titleImg)){
            return Result.success("ok");
        }else {
            return Result.error("添加错误！");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="competitionId",value="根据此Id修改比赛信息",required=true,paramType = "query"),
            @ApiImplicitParam(name="publisherId",value="发布者的ID",paramType = "query"),
            @ApiImplicitParam(name="competitionName",value="比赛的名字",paramType = "query"),
            @ApiImplicitParam(name="nickName",value="昵称",paramType = "query"),
            @ApiImplicitParam(name="officialUrl",value = "官网的Url",paramType = "query"),
            @ApiImplicitParam(name="infoDetail",value = "详细内容",paramType = "query"),
            @ApiImplicitParam(name="registerDetail",value = "报名内容",paramType = "query"),
//            @ApiImplicitParam(name="imgFile",value = "封面的图片",paramType = "form"),
//            @ApiImplicitParam(name="titleImg",value = "竞赛头像",paramType = "form")
    })
    @PostMapping("/update")
    @ApiOperation("更新赛事信息")
    public Result updateCompetition(Integer competitionId, @RequestParam(required = false) Integer publisherId, @RequestParam(required = false) String competitionName, @RequestParam(required = false) String nickName,
                                    @RequestParam(required = false) String officialUrl , @RequestParam(required = false) String infoDetail,
                                    @RequestParam(required = false) String registerDetail , @RequestParam(required = false)MultipartFile imgFile,
                                    @RequestParam(required = false) MultipartFile titleImg){
        if(competitionService.updateCompetition(competitionId,publisherId,competitionName,nickName,officialUrl,infoDetail,registerDetail,imgFile,titleImg)){
            return Result.success("ok");
        }else {
            return Result.error("更新错误！");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="competitionId",value = "比赛的ID",paramType = "query"),
            @ApiImplicitParam(name="departmentId",value = "专业的ID",paramType = "query")
    })
    @GetMapping("/addTag")
    @ApiOperation("管理员为比赛增添专业推荐的tag")
    public Result insertTag(Integer competitionId, Integer departmentId){
        if(competitionService.insertTag(competitionId,departmentId)){
            return Result.success("添加tag成功");
        }else {
            return Result.error("添加失败！");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="tagId",value = "比赛和专业之间连接的tag的ID",paramType = "query")
    })
    @GetMapping("/deleteTag")
    @ApiOperation("管理员删除比赛和专业之间的tag")
    public Result deleteTag(Integer tagId){
        if(competitionService.deleteTag(tagId)){
            return Result.success("删除成功！");
        }else {
            return Result.error("删除失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="comId",value="比赛的ID",paramType = "query"),
            @ApiImplicitParam(name="competitionName",value="比赛的名字",paramType = "query"),
            @ApiImplicitParam(name="nickName",value="比赛的昵称",paramType = "query"),
            @ApiImplicitParam(name="departmentName",value="专业名称，传入可搜索与此专业相关的比赛",paramType = "query"),
//            @ApiImplicitParam(name="pageNum",value = "页号",paramType = "query"),
//            @ApiImplicitParam(name="pageSize",value = "页面大小",paramType = "query")
//          @ApiImplicitParam(name="imgFile",value = "上传的图片",paramType = "File")
    })
    @PostMapping("/queryCompetitionDetail")
    @ApiOperation("根据条件查询比赛详情")
    public List<Competition> queryCompetition(@RequestParam(required = false) Integer comId, @RequestParam(required = false) String competitionName,
                                              @RequestParam(required = false) String nickName, @RequestParam(required = false) String departmentName){
//                                              @RequestParam(required = false,defaultValue = "1")Integer pageNum, @RequestParam(required = false,defaultValue = "10") Integer pageSize){
         return competitionService.queryCompetition(comId,competitionName,nickName,departmentName,0,0);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="comId",value = "比赛对应的ID",paramType = "query")
    })
    @GetMapping("/deleteCompetition")
    @ApiOperation("根据比赛的id删除比赛")
    public Result deleteCompetition(Integer comId){
        if(competitionService.deleteCompetition(comId)){
            return Result.success("ok");
        }else {
            return Result.error("删除失败");
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name="comId",value="比赛的ID",paramType = "query"),
            @ApiImplicitParam(name="competitionName",value="比赛的名字",paramType = "query"),
            @ApiImplicitParam(name="nickName",value="比赛的昵称",paramType = "query"),
            @ApiImplicitParam(name="departmentName",value="专业名称，传入可搜索与此专业相关的比赛",paramType = "query")
    })
    @ApiOperation("分页查询比赛")
    @PostMapping("/queryCompetition")
    public List<CompetitionBrief> queryBriefCompetition(@RequestParam(required = false) Integer comId, @RequestParam(required = false) String competitionName,
                                                        @RequestParam(required = false) String nickName, @RequestParam(required = false) String departmentName,
                                                        @RequestParam(required = false,defaultValue = "1")Integer pageNum, @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
        return competitionService.queryBriefCompetition(comId,competitionName,nickName,departmentName,pageNum,pageSize);
    }

}
